# This is a file which defines the attributes of this agent.
# Please follow the example.
#
# NB! For determining the function name we are parsing the source code. Please
# do not put a spacebar between the function name and parenthesis.
# NNB! In the name (e.g. Phrase length (standard)), the suffix (standard) refers
# to the fact, that these are methods, which come with the original source.
# For your own methods not add anything to the parenthesis (e.g. the example of numberOfWords)
# NNB! Please add your first name and first letter of your last name in front of 
# the function name. This is for avoiding clashing of the attribute names.
#
<attribute>
<name>
phrase length (standard)
</name>
	<function>
def phraseLength(phrase):
	return len(phrase)
	</function>
</attribute>

<attribute>
<name>
phrase vowels (standard)
</name>
<function>
def phraseVowels(phrase):
	vowels = "aeiou"
	return len(filter(lambda x: x in vowels, phrase))
</function>
</attribute>

<attribute>
<name>
phrase consonants (standard)
</name>
<function>
def phraseConsonants(phrase):
	consonants = "bcdfghjklmnpqrstv"
	return len(filter(lambda x: x in consonants, phrase))
	</function>
</attribute>


########## Meysam
<attribute>
<name>
word pronunciation
</name>
<function>
def MeysamM_wordPronunc(wordIn):
	import nltk
	entr = nltk.corpus.cmudict.entries()
	return [pronunc for word, pronunc in entr if word==wordIn]
	</function>
</attribute>

<attribute>
<name>
phrase ending rhyme
</name>
<function>
def MeysamM_endingRhyme(phraseIn):
	words = phraseIn.split(' ')
	pronuncs = words
	for i in range(0,len(pronuncs)):
		pronuncs[i] = []
	minLen = 100
	
	import nltk
	entries = nltk.corpus.cmudict.entries()
	for i in range(len(pronuncs)):
		for (w,p) in entries:
			if w==words[i]:
				pronuncs[i] = p
				if len(pronuncs[i])<minLen:
					minLen = len(pronuncs[i])
				break		
	
	pattern = pronuncs.pop()
	for rhymeLen in range(minLen, 0, -1):
		rhyme = 0
		for pron in pronuncs:
			if pron == pattern:
				continue
			if pron[-rhymeLen:]==pattern[-rhymeLen:]:
				rhyme = 1
			else:
				rhyme=0
				break
		if rhyme==1:
			return rhymeLen
			
	return 0
	</function>
</attribute>


<attribute>
<name>
phrase beginning rhyme
</name>
<function>
def MeysamM_beginningRhyme(phraseIn):
	words = phraseIn.split(' ')
	pronuncs = words
	for i in range(0,len(pronuncs)):
		pronuncs[i] = []
	minLen = 100
	
	import nltk
	entries = nltk.corpus.cmudict.entries()
	for i in range(len(pronuncs)):
		for (w,p) in entries:
			if w==words[i]:
				pronuncs[i] = p
				if len(pronuncs[i])<minLen:
					minLen = len(pronuncs[i])
				break		
	
	pattern = pronuncs.pop()
	for rhymeLen in range(minLen, 0, -1):
		rhyme = 0
		for pron in pronuncs:
			if pron == pattern:
				continue
			if pron[0:rhymeLen]==pattern[0:rhymeLen]:
				rhyme = 1
			else:
				rhyme=0
				break
		if rhyme==1:
			return rhymeLen
			
	return 0
	</function>
</attribute>



<attribute>
<name>
matching letters
</name>
<function>
def MeysamM_matchingLetters(phraseIn):
	words = phraseIn.split(' ')
	maxMatch = 0
	for w1 in range(0,len(words)):
		for w2 in range(w1+1,len(words)):
			if words[w1]!=[] and words[w2]!=[] and words[w1]!=words[w2]:
				i = j = last_j = count = 0
				while i<len(words[w1]) and j<len(words[w2]):
					if words[w1][i]==words[w2][j]:
						count += 1
						i += 1
						j += 1
						last_j = j
						if  j>=len(words[w2]):
							if last_j<len(words[w2]):
								j = last_j
							else:
								break
					else:
						j += 1
						if  j>=len(words[w2]):
							i += 1
							j = last_j	
				if count>maxMatch:
					maxMatch = count
	#In some case, the order of words affect the result
	#e.g. the result for 'fabc abcf' is 1 but for 'abcf fabc' is 3
	temp = range(0,len(words))
	i = len(words)-1
	for w in words:
		temp[i] = w
		i -= 1
	words = temp
	for w1 in range(0,len(words)):
		for w2 in range(w1+1,len(words)):
			if words[w1]!=[] and words[w2]!=[] and words[w1]!=words[w2]:
				i = j = last_j = count = 0
				while i<len(words[w1]) and j<len(words[w2]):
					if words[w1][i]==words[w2][j]:
						count += 1
						i += 1
						j += 1
						last_j = j
						if  j>=len(words[w2]):
							if last_j<len(words[w2]):
								j = last_j
							else:
								break
					else:
						j += 1
						if  j>=len(words[w2]):
							i += 1
							j = last_j
						
				if count>maxMatch:
					maxMatch = count
	return maxMatch
	</function>
</attribute>



<attribute>
<name>
matching letters
</name>
<function>
def MeysamM_matchingVowelsConsents(phraseIn):
	words = phraseIn.split(' ')
	wordsVowels = []
	wordsConsonents = []
	vowels = "aeiou"
	for word in words:
		v = []
		c = []
		for l in word:
			if l in vowels:
				v.append(l)
			else:
				c.append(l)
		wordsVowels.append(v)
		wordsConsonents.append(c)
	maxMatch = 0
	for w1 in range(0,len(words)):
		for w2 in range(w1+1,len(words)):
			#matching vowels:
			if wordsVowels[w1]!=[] and wordsVowels[w2]!=[]:
				i = j = last_j = count = 0
				while i<len(wordsVowels[w1]) and j<len(wordsVowels[w2]):
					if wordsVowels[w1][i]==wordsVowels[w2][j]:
						count += 1
						i += 1
						j += 1
						last_j = j
					else:
						j += 1
					if  j>=len(wordsVowels[w2]):
						i += 1
						j = last_j
						
				if count>maxMatch:
					maxMatch = count
			#matching consonents:
			if wordsConsonents[w1]!=[] and wordsConsonents[w2]!=[]:
				i = j = last_j = count  = 0
				while i<len(wordsConsonents[w1]) and j<len(wordsConsonents[w2]):
					if wordsConsonents[w1][i]==wordsConsonents[w2][j]:
						count += 1
						i += 1
						j += 1
						last_j = j
					else:
						j += 1
					if  j>=len(wordsConsonents[w2]):
						i += 1
						j = last_j
				if count>maxMatch:
					maxMatch = count
	return maxMatch
	</function>
</attribute>
